package Code.Test;

import java.util.Scanner;

//需求：定义一个方法copyOfRange(int[] arr,int from int to)
//功能：将数组arr中索引from（包含from）开始到索引to（不包含to）的元素复制到新数组中，将数组返回
public class Test04 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入from：");
        int from = scanner.nextInt();
        System.out.println("请输入to：");
        int to = scanner.nextInt();

        if (from>=0&&to>from&&to<= arr.length){

            int[] newArr = copyOfRange(arr,from,to);

            Test1.Traversal(newArr);
        }else {
            System.out.println("输入失败");
        }



    }
    public static int[] copyOfRange(int[] arr,int from,int to){
        int[] newArr = new int[to - from];

        int index = 0;
        for (int i = from; i < to; i++) {
            newArr[index] = arr[i];

            index++;
        }
        return newArr;
    }
}
